package com.backwaveweibo.bww.service.impl;

import com.backwaveweibo.bww.entity.User;
import com.backwaveweibo.bww.mapper.UserMapper;
import com.backwaveweibo.bww.service.FollowService;
import com.backwaveweibo.bww.service.UserService;
import com.backwaveweibo.bww.utils.FileUploadWithQiNiuYun;
import com.jn.sqlhelper.dialect.pagination.PagingRequest;
import com.jn.sqlhelper.dialect.pagination.PagingRequestContextHolder;
import com.jn.sqlhelper.dialect.pagination.PagingResult;
import com.jn.sqlhelper.dialect.pagination.SqlPaginations;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    FollowService followService;

    public User findUser(User user) {
        return userMapper.selectUser(user);
    }

    public Boolean addUser(User user) {
        User selectUser = userMapper.selectUser(user);
        if (selectUser == null) {
            user.setType("user");
            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strDateFormat);
            user.setRegisterDate(simpleDateFormat.format(date));
            user.setPassword(new Sha1Hash(user.getPassword(), user.getUsername(), 2).toString());
            user.setPhoto(FileUploadWithQiNiuYun.DOMAIN + "MM.jpg");
            Integer integer = userMapper.addUser(user);
            return integer == 1;
        }
        return false;
    }

    public Boolean updateUser(User user) {
        if (user.getPassword() != null) {
            user.setPassword(new Sha1Hash(user.getPassword(), user.getUsername(), 2).toString());
        }
        Integer integer = userMapper.updateUser(user);
        return integer == 1;
    }


    @Transactional
    public Boolean deleteUser(Long id)  {
        log.info("执行了删除用户方法==>被删除用户id:{}", id);
        List<Long> myFans = followService.getMyFans(new User().setUserId(id));
        List<Long> mySubscribe = followService.getMySubscribe(id);
        Boolean fansUnfollow = false, unSubscribe = false;
        Integer integer;

        integer = userMapper.deleteUser(id);

        if (myFans.size() > 0) {
            for (Long fansId : myFans) {
                fansUnfollow = followService.setUnFollow(id, new User().setUserId(fansId));
            }
        }
        if (mySubscribe.size() > 0) {
            for (Long subscribeId : mySubscribe) {
                unSubscribe = followService.setUnFollow(subscribeId, new User().setUserId(id));
            }
        } if (mySubscribe.size() == 0){
            unSubscribe = true;
        } if (myFans.size() == 0){
            fansUnfollow = true;
        }

        Boolean delUserKey = followService.delUserKey(id);
        return integer == 1 && fansUnfollow && unSubscribe && delUserKey;
    }

    @Override
    public List<User> batchSelectUser(List<Long> userId) {
        return userMapper.selectUserBatch(userId);
    }

    @Override
    public Boolean updatePassword(Long id, String originalPassword, String newPassword) {
        log.info("执行了更改密码方法");
        User user = userMapper.selectUser(new User().setUserId(id));
        log.info(String.valueOf(user));
        if (user.getPassword().equals(new Sha1Hash(originalPassword, user.getUsername(), 2).toString())) {
            Integer integer = userMapper.updateUser(new User().setUserId(id).setPassword(new Sha1Hash(newPassword, user.getUsername(), 2).toString()));
            return integer == 1;
        }
        return false;
    }


    public PagingResult findUserPage(Integer pageNumber, Integer pageSize, User user) {

        PagingRequest PagingRequest = SqlPaginations.preparePagination(pageNumber == null ? 1 : pageNumber, pageSize == null ? 1 : pageSize);
        PagingRequestContextHolder.getContext().setPagingRequest(PagingRequest);
        List<User> users = userMapper.selectUserPage(user);
        return PagingRequest.getResult().setItems(users);
    }
}
